﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Printing;
using System.Windows.Shapes;

namespace mvvmCommon
{
    public class mvvmActionMenuViewModel : ViewModelBase
    {
        public mvvmActionMenuViewModel()
        {
            ActionMenuButton.actionControl = this;
            this.defaultAction = new DefaultAction(this);
            pd = new PrintDocument();
            
            pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);
        }

        public DefaultAction defaultAction;
        private PrintDocument pd;
        public UIElement printObject;
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
               // Grid gr = new Grid();
                //Grid gr = printObject as Grid;
                //gr.HorizontalAlignment = HorizontalAlignment.Center;
                //gr.VerticalAlignment = VerticalAlignment.Center;
                //gr.Margin = new Thickness(20, 50, 20, 0);
                //gr.Children.Add(a);
                
                e.PageVisual = printObject;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
        }

        #region[set actions]

        public virtual void SetAllAction(ICommand command)
        {
            
        }

        public void SetAllDisable()
        {
            this.Update = this.defaultAction;
            this.Insert = this.defaultAction;
            this.Delete = this.defaultAction;
            this.Search = this.defaultAction;
            this.Edit = this.defaultAction;
            this.View = this.defaultAction;
            this.Approve = this.defaultAction;
            this.Close = this.defaultAction;
        }

        public virtual void SetAllAction(ICommand insert, ICommand update, ICommand delete, ICommand search, ICommand edit)
        {            
        }
        public virtual void SetAllAction(ICommand insert, ICommand update, ICommand delete, ICommand search, ICommand edit, ICommand view, ICommand approve)
        {
            
        }
        public virtual void SetAllAction(ICommand insert, ICommand update, ICommand delete, ICommand search, ICommand edit, ICommand view, ICommand approve, ICommand close)
        {
            
        }
        public virtual void SetEnterAction(ICommand command)
        {            
        }

        public void PrintScreen(string fileName, UIElement printObject)
        {
            this.printObject = printObject;
            pd.Print(fileName);
        }
        public void PrintDefault()
        {
            pd.Print("Default");
        }

        #endregion

        #region[Text for Command Parameter]

        public object UpdateCommandParameter
        {
            get
            {
                return ActionMenuButton.Update;
            }
        }
        public object DeleteCommandParameter
        {
            get
            {
                return ActionMenuButton.Delete;
            }
        }
        public object InsertCommandParameter
        {
            get
            {
                return ActionMenuButton.Insert;
            }
        }
        public object SearchCommandParameter
        {
            get
            {
                return ActionMenuButton.Search;
            }
        }
        public object EditCommandParameter
        {
            get
            {
                return ActionMenuButton.Edit;
            }
        }
        public object ViewCommandParameter
        {
            get
            {
                return ActionMenuButton.View;
            }
        }
        public object ApproveCommandParameter
        {
            get
            {
                return ActionMenuButton.Approve;
            }
        }
        public object CloseCommandParameter
        {
            get
            {
                return ActionMenuButton.Close;
            }
        }

        #endregion

        #region [Action Button]

        private ICommand update;
        public ICommand Update
        {
            get
            {
                return this.update;
            }
            set
            {
                this.update = value;
                this.OnPropertyChanged("Update");
            }
        }

        private ICommand delete;
        public ICommand Delete
        {
            get
            {
                return this.delete;
            }
            set
            {
                this.delete = value;
                this.OnPropertyChanged("Delete");
            }
        }

        private ICommand insert;
        public ICommand Insert
        {
            get
            {
                return this.insert;
            }
            set
            {
                this.insert = value;
                this.OnPropertyChanged("Insert");
            }
        }

        private ICommand search;
        public ICommand Search
        {
            get
            {
                return this.search;
            }
            set
            {
                this.search = value;
                this.OnPropertyChanged("Search");
            }
        }

        private ICommand edit;
        public ICommand Edit
        {
            get
            {
                return this.edit;
            }
            set
            {
                this.edit = value;
                this.OnPropertyChanged("Edit");
            }
        }

        private ICommand view;
        public ICommand View
        {
            get
            {
                return this.view;
            }
            set
            {
                this.view = value;
                this.OnPropertyChanged("View");
            }
        }

        private ICommand approve;
        public ICommand Approve
        {
            get
            {
                return this.approve;
            }
            set
            {
                this.approve = value;
                this.OnPropertyChanged("Approve");
            }
        }

        private ICommand close;
        public ICommand Close
        {
            get
            {
                return this.close;
            }
            set
            {
                this.close = value;
                this.OnPropertyChanged("Close");
            }
        }

        #endregion       

        public class DefaultAction : ICommand
        {
            private ViewModelBase viewModel;
            public DefaultAction(ViewModelBase viewModel)
            {
                this.viewModel = viewModel;
                this.viewModel.PropertyChanged += (s, e) =>
                {
                    if (this.CanExecuteChanged != null)
                    {
                        this.CanExecuteChanged(this, new EventArgs());
                    }
                };
            }

            public bool CanExecute(object parameter)
            {
                return false;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                throw new NotImplementedException();
            }
        }
        
    }
    
}
